package controller;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: Admin
 * Date: 2023-01-19
 * Time: 16:51
 */
@WebServlet("/logOut")
public class LogOutServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        HttpSession session = req.getSession(false);

        //前面是判断会话和用户是否存在,只需要破坏其中一个条件就可以表示退出登录,所以没有会话信息想删也没得删,默认就是退出登录了
        if(session == null) {
            resp.sendRedirect("login.html");
            return;
        }

        //删除用户会话
        session.removeAttribute("user");
        resp.sendRedirect("login.html");
    }
}
